_ = require('underscore')
page_zygg = (state={},action)->
  switch action.type
    when 'ZYGG/GET_LIST_START'
      Object.assign {},state,{isLoading:true}
    when 'ZYGG/GET_LIST_COMPLETED'
      Object.assign {},state,{
        list:state.list.concat(action.payload.list)
        isLoading:false
        pageIndex:if action.payload.list.length > 0 then state.pageIndex+1 else state.pageIndex
        completed:action.payload.list.length < 20
        lastFetchTime:action.payload.lastFetchTime
      }
    when 'ZYGG/REFRESH_LIST_START'
      Object.assign {},state,{
        list:[]
        isLoading:false
        refreshing:true
      }
    when 'ZYGG/REFRESH_LIST_COMPLETED'
      Object.assign {},state,{
        list:action.payload.list
        isLoading:false
        pageIndex:1
        refreshing:false
        completed:action.payload.list.length<20
      }
    when 'ZZGG/SET_MARK_READ'
      debugger
      array = state.markRead ? []
      array = array.concat()
      if _.contains(array,action.payload)
        return state
      else
        array.push action.payload
        Object.assign {},state,
          markRead:array
    else
      state

module.exports = page_zygg
